package com.braveman.im.ui.weiget;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.braveman.im.R;
import com.braveman.im.utils.PermissionUtils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import razerdp.basepopup.BasePopupWindow;

/**
 * Created by 大灯泡 on 2016/1/22.
 * 菜单。
 */
public class QrCodePopup extends BasePopupWindow implements View.OnClickListener {

    private Activity context;
    private LinearLayout mRlMemberPhoto;
    private String url;


    public QrCodePopup(Activity context,String url) {
        super(context, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//        findViewById(R.id.iv_activity_shop_collection_qrcode).setOnLongClickListener(new View.OnLongClickListener() {
//            @Override
//            public boolean onLongClick(View view) {
//                return false;
//            }
//        });
        mRlMemberPhoto =  (LinearLayout)getPopupWindowView().findViewById(R.id.popup_contianer_show);
        this.context = context;
        this.url = url;
    }

    @Override
    protected Animation initShowAnimation() {
//        AnimationSet set = new AnimationSet(true);
//        set.setInterpolator(new DecelerateInterpolator());
//        set.addAnimation(getScaleAnimation(0, 1, 0, 1, Animation.RELATIVE_TO_SELF, 1, Animation.RELATIVE_TO_SELF, 0));
//        set.addAnimation(getDefaultAlphaAnimation());
//        return set;
        return null;
    }

    @Override
    public Animator initShowAnimator() {
        AnimatorSet set = new AnimatorSet();
        set.playTogether(
                ObjectAnimator.ofFloat(mAnimaView, "scaleX", 0.0f, 1.0f).setDuration(300),
                ObjectAnimator.ofFloat(mAnimaView, "scaleY", 0.0f, 1.0f).setDuration(300),
                ObjectAnimator.ofFloat(mAnimaView, "alpha", 0.0f, 1.0f).setDuration(300 * 3 / 2));
        return null;
    }

    @Override
    public void showPopupWindow(View v) {
        setOffsetX(-(getWidth() - v.getWidth() / 2));
        setOffsetY(-v.getHeight() / 2);
        super.showPopupWindow(v);
    }

    @Override
    public View getClickToDismissView() {
        return null;
    }

    @Override
    public View onCreatePopupView() {
        return createPopupById(R.layout.activity_qr_code_formine);
    }

    @Override
    public View initAnimaView() {
        return getPopupWindowView().findViewById(R.id.popup_contianer_show);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

        }

    }

    //保存图片到系统相册
    private void saveImg() {
        mRlMemberPhoto.buildDrawingCache();
        Bitmap bitmap = mRlMemberPhoto.getDrawingCache();
        //6.0动态申请权限
        PermissionUtils.checkPermission(context, "android.permission.WRITE_EXTERNAL_STORAGE");
        //将Bitmap 转换成二进制，写入本地
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
        byte[] byteArray = stream.toByteArray();
        File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/钱包++");
        if (!dir.isFile()) {
            dir.mkdir();
        }
        File file = new File(dir, "QR_CODE.png");
        try {
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(byteArray, 0, byteArray.length);
            fos.flush();
            // 把文件插入到系统图库
            try {
                MediaStore.Images.Media.insertImage(context.getContentResolver(),
                        file.getAbsolutePath(), "QR_CODE.png", null);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            // 通知图库更新
            Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            Uri uri = Uri.fromFile(file);
            intent.setData(uri);
            context.sendBroadcast(intent);
            Toast.makeText(context.getApplicationContext(), "保存成功", Toast.LENGTH_SHORT).show();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();

        }
    }


}
